<template>
  <div class="tags">
    <el-tag v-for="(tag, index) in tags" :key="tag.path" :closable="tag.name !== 'home'" size="small"
            :effect="$route.name === tag.name ? 'dark' : 'plain'"
      @close="handleClose(tag, index)" @click="handleClick(tag)">
      {{tag.label}}
    </el-tag>
  </div>
</template>

<script>
import {mapState} from "vuex";

export default {
  data() {
    return {
    }
  },
  methods: {
    handleClose(tag, index) {
      const length = this.tags.length - 1
      this.$store.commit('closeTag', tag)
      if (tag.name !== this.$route.name) {
        return;
      }
      if (index === length) {
        this.$router.push({
          name: this.tags[index-1].name
        })
      } else {
        this.$router.push({
          name: this.tags[index].name
        })
      }
    },
    handleClick(tag) {
      this.$router.push({ name: tag.name })
    }
  },
  computed: {
    ...mapState({
      // 把tabList的值赋给tags
      tags: state => state.tab.tabList
    })
  }
}
</script>

<style lang="less" scoped>
  .tags {
    margin: 10px 0 10px 10px;
    .el-tag {
      cursor: pointer;
    }
    // "+" 相邻兄弟选择器,该选择器能够匹配指定元素后面相邻的兄弟元素。”
    .el-tag + .el-tag {
      margin-left: 10px;
    }
  }
</style>